<template>
  <el-card class="box-card">
    <el-form ref="queryForm" :model="query" :inline="true">
      <el-form-item label="资源名称" prop="resourcesName">
        <el-input v-model.trim="query.conditions.resourcesName" placeholder="请输入资源名称" clearable @change="handleQuery" />
      </el-form-item>
      <el-form-item label="资源类型">
        <el-select style="width: 100px" v-model="query.conditions.msgType" placeholder="全部">
          <el-option label="图片" value="image"></el-option>
          <el-option label="图文" value="image_text"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select style="width: 100px" v-model="query.conditions.publishState" placeholder="全部">
          <el-option label="未发布" :value="0"></el-option>
          <el-option label="已发布" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb20">
      <el-col :span="1.5">
        <el-button v-has-permi="['/wx/menu/postReplyMsgAdd']" type="primary" @click="handleAdd(null, 'image')">新增图片</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-has-permi="['/wx/menu/postReplyMsgAdd']" type="primary" @click="handleAdd(null, 'image_text')">新增图文</el-button>
      </el-col>
    </el-row>

    <el-card class="box-card">
      <el-table v-loading="listLoading" :data="dataList" stripe border row-key="id">
        <el-table-column label="资源名称" align="center" prop="resourcesName" />
        <el-table-column label="资源类型" align="center">
          <template slot-scope="{row}">
            <span v-if="row.msgType==='image'">图片</span>
            <span v-else>图文</span>
          </template>
        </el-table-column>
        <el-table-column label="发布状态" align="center">
          <template slot-scope="{row}">
            <span v-if="row.publishState===1">已发布</span>
            <span v-else>未发布</span>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" align="center">
          <template slot-scope="{row}">
            <span>{{ row.publishTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button v-has-permi="['/wx/menu/postUpdateWxTag']" type="text" @click="handleState(row)">
              <span v-if="row.publishState===1">取消发布</span>
              <span v-else>发布</span>
            </el-button>
            <el-button v-if="row.publishState!==1" v-has-permi="['/wx/menu/postReplyMsgUpdate']" type="text" @click="handleAdd(row)">编辑</el-button>
            <el-button v-if="row.publishState!==1" v-has-permi="['/wx/menu/postMaterialDelete']" type="text" @click="handleDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="query.pageNum" :limit.sync="query.pageSize" @pagination="handleQuery" />
    </el-card>

    <!-- 添加或修改菜单 -->
    <el-dialog :title="(formModel.id?'编辑':'新增') + (formModel.msgType==='image'?'图片':'图文')" :visible.sync="formVisible" width="600px" append-to-body>
      <el-form ref="formModel" :model="formModel" :rules="formModelRules" label-width="110px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="资源名称:" prop="resourcesName">
              <el-input v-model.trim="formModel.resourcesName" style="width: 90%" clearable placeholder="请输入资源名称" maxlength="12" />
            </el-form-item>
          </el-col>
          <el-col v-if="formModel.msgType==='image_text'" :span="24">
            <el-form-item label="标题:" prop="title">
              <el-input v-model.trim="formModel.title" style="width: 90%" clearable placeholder="请输入标题" maxlength="20" />
            </el-form-item>
          </el-col>
          <el-col v-if="formModel.msgType==='image_text'" :span="24">
            <el-form-item label="描述:" prop="description">
              <el-input v-model.trim="formModel.description" type="textarea" style="width: 90%" :autosize="{ minRows: 4, maxRows: 6}" clearable placeholder="请输入描述" maxlength="100" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片:" prop="picUrlAzure">
              <upload-img v-model="formModel.picUrlAzure" />
            </el-form-item>
          </el-col>
          <el-col v-if="formModel.msgType==='image_text'" :span="24">
            <el-form-item label="链接:" prop="url">
              <el-input v-model.trim="formModel.url" style="width: 90%" clearable placeholder="请输入链接" maxlength="200" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible=!formVisible;reset;">取 消</el-button>
        <el-button :loading="submitLoading" type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { postReplyMsgList, postReplyMsgAdd, postMaterialDelete, postReplyMsgUpdate } from '@/api/wechatService/resource'

export default {
  name: "index",
  data () {
    return {
      total: 0,
      // 列表数据
      query: {
        pageNum: 1,
        pageSize: 10,
        conditions: {
          resourcesName: '',
          msgType: ''
        }
      },
      dataList: [],
      listLoading: false,
      // 新增/编辑用户
      formVisible: false,
      submitLoading: false,
      formModel: {},
      formModelRules: {
        resourcesName: [
          { required: true, message: "资源名称不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        picUrlAzure: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        url: [
          { required: true, message: "链接不能为空", trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    handleQuery () {
      this.listLoading = true
      let params = {...this.query}
      postReplyMsgList(params).then(res => {
        this.dataList = res.data.records
        this.total = res.data.total
        this.listLoading = false
      }).catch(err => {
        if (err.code === -103) {
          this.$router.push('/403')
        }
        this.listLoading = false
      })
    },
    resetQuery () {
      this.query = {
        pageNum: 1,
        pageSize: 10,
        conditions: {
          resourcesName: '',
          msgType: ''
        }
      }
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleAdd(row, msgType) {
      this.reset()
      if (msgType) {
        this.$set(this.formModel, 'msgType', msgType)
      }
      if (row) {
        this.formModel = {...row}
      }
      this.formVisible = !this.formVisible
    },
    // 发布/取消发布
    handleState({id,publishState}) {
      this.$confirm(`请确认是否要${publishState===1?'发布':'取消发布'}该资源?`, { type: 'warning' }).then(() => {
        postReplyMsgUpdate({ id, publishState: publishState===1?0:1 }).then(() => {
          this.$message.success(`${publishState===1?'取消':'发布'}成功`)
          this.handleQuery()
        })
      })
    },
    // 删除
    handleDelete (id) {
      this.$confirm(`请确认是否要删除该资源?`, { type: 'warning' }).then(() => {
        postMaterialDelete({ id }).then(() => {
          this.$message.success('删除成功')
          this.handleQuery()
        })
      })
    },
    // 新增/编辑
    submitForm () {
      this.submitLoading = true
      this.$refs["formModel"].validate(async valid => {
        if (valid) {
          let params = {...this.formModel}
          try {
            if (params.id) {
              await postReplyMsgUpdate(params)
              this.$message.success('修改成功')
            } else {
              await postReplyMsgAdd(params)
              this.$message.success('新增成功')
            }
            this.formVisible = !this.formVisible
            this.handleQuery()
            this.reset()
            this.submitLoading = false
          } catch (error) {
            this.submitLoading = false
          }
        } else {
          this.submitLoading = false
        }
      })
    },
    // 表单重置
    reset () {
      this.formModel = {}
      this.resetForm("formModel")
    },
  },
  mounted () {
    this.handleQuery()
  },
}
</script>

<style scoped>
.dh {
  width: 0;
  height: 100px;
  background-color: #1c84c6;
  transition: all 1s;
}
.dh.show {
  width: 200px;
}
.dh.hide {
  width: 0;
}
</style>
